// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Plotter.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartGraphics;
import com.java4less.rchart.gc.ChartImage;
import java.util.Calendar;
import java.util.Vector;

// Referenced classes of package com.java4less.rchart:
//			ChartComponent, BarPlotter3D, LinePlotter3D, DataSerie, 
//			LinePlotter, BarPlotter, RadarPlotter, FillStyle, 
//			Chart, Scale, Axis

public class Plotter extends ChartComponent
{

	protected int needsAxis;
	protected boolean combinable;
	public Scale XScale;
	public Scale YScale;
	public Scale Y2Scale;
	public FillStyle back;
	public ChartImage backImage;
	public int visibleWidth;
	public int visibleHeight;
	public int depth;
	Vector series;

	public Plotter()
	{
		needsAxis = 2;
		combinable = true;
		back = null;
		visibleWidth = 0;
		visibleHeight = 0;
		depth = 0;
		series = new Vector(0, 1);
	}

	public int getNeedsAxis()
	{
		return needsAxis;
	}

	public boolean getCombinable()
	{
		return combinable;
	}

	public void plotBackground(ChartGraphics g, int bw, int bh, int offsetX, int offsetY)
	{
		if (back != null)
		{
			boolean D3 = false;
			if (this instanceof BarPlotter3D)
				D3 = true;
			if (this instanceof LinePlotter3D)
				D3 = true;
			if (D3)
			{
				int xs[] = new int[6];
				int ys[] = new int[6];
				xs[0] = x + offsetX;
				ys[0] = y + offsetY;
				xs[1] = x + offsetX + depth;
				ys[1] = (y + offsetY) - depth;
				xs[2] = x + offsetX + visibleWidth;
				ys[2] = (y + offsetY) - depth;
				xs[3] = x + offsetX + visibleWidth;
				ys[3] = (y + offsetY + visibleHeight) - depth - depth;
				xs[4] = (x + offsetX + visibleWidth) - depth;
				ys[4] = (y + offsetY + visibleHeight) - depth;
				xs[5] = x + offsetX;
				ys[5] = (y + offsetY + visibleHeight) - depth;
				back.drawPolygon(g, xs, ys, 6);
			} else
			{
				back.draw(g, x, y, x + bw, y + bh);
			}
		}
		if (backImage != null)
		{
			int w = backImage.getWidth();
			int h = backImage.getHeight();
			if (w > -1 && h > -1)
			{
				int toCenterX = (bw - w) / 2;
				if (toCenterX < 0)
					toCenterX = 0;
				int toCenterY = (bh - h) / 2;
				if (toCenterY < 0)
					toCenterY = 0;
				g.drawImage(backImage, toCenterX + x, y + toCenterY);
			}
		}
	}

	public void plot(ChartGraphics g)
	{
		for (int i = 0; i < series.size(); i++)
		{
			DataSerie s = (DataSerie)series.elementAt(i);
			plotSerie(g, s, i);
		}

	}

	protected void plotSerie(ChartGraphics chartgraphics, DataSerie dataserie, int i)
	{
	}

	public int getSeriesCount()
	{
		return series.size();
	}

	public DataSerie getSerie(int p)
	{
		return (DataSerie)series.elementAt(p);
	}

	public void setSerie(int p, DataSerie s)
	{
		if (p < series.size())
			series.setElementAt(s, p);
	}

	public void addSerie(DataSerie s)
	{
		replaceSerie(-1, s);
	}

	public void replaceSerie(int p, DataSerie s)
	{
		Scale tmpScaleX = getActiveXScale(s);
		Scale tmpScaleY = getActiveYScale(s);
		if (p >= series.size())
			return;
		int month = Calendar.getInstance().get(2);
		if (Chart.d() != 1 && s.getSize() > 10)
			return;
		if (Chart.d() != 1 && series.size() > 3)
			return;
		if (p == -1)
			series.addElement(s);
		else
			series.setElementAt(s, p);
		boolean fixedLimits = false;
		if (this instanceof LinePlotter)
			fixedLimits = ((LinePlotter)this).fixedLimits;
		if (this instanceof LinePlotter3D)
			fixedLimits = ((LinePlotter3D)this).fixedLimits;
		boolean cumulative = false;
		if ((this instanceof BarPlotter) && ((BarPlotter)this).cumulative && !((BarPlotter)this).cumulativeBackwardsCompatible)
			cumulative = true;
		if ((this instanceof BarPlotter3D) && ((BarPlotter3D)this).cumulative)
			cumulative = true;
		if (!(this instanceof RadarPlotter))
		{
			for (int i = 0; i < s.getSize(); i++)
			{
				if (s.getElementY(i) == null)
					continue;
				double XValue = ((Double)s.getElementX(i)).doubleValue();
				double YValue = ((Double)s.getElementY(i)).doubleValue();
				if (cumulative)
				{
					YValue = 0.0D;
					for (int si = 0; si < series.size(); si++)
					{
						DataSerie ser = (DataSerie)series.elementAt(si);
						if (inSameSubChart(ser, s) && ser.getSize() > i && ser.getElementY(i) != null)
						{
							double d = ((Double)ser.getElementY(i)).doubleValue();
							YValue += d;
						}
					}

				}
				if (XValue >= tmpScaleX.max)
					if ((this instanceof BarPlotter) || (this instanceof BarPlotter3D))
					{
						tmpScaleX.max = XValue + 1.0D;
						if ((this instanceof BarPlotter) && ((BarPlotter)this).barAtAxis)
							tmpScaleX.max = XValue;
					} else
					{
						calculateNewMax(tmpScaleX, XValue);
					}
				if (XValue < tmpScaleX.min)
					calculateNewMin(tmpScaleX, XValue);
				if (fixedLimits)
					continue;
				if (s.secondYAxis && Y2Scale != null)
				{
					if (YValue > Y2Scale.max)
						calculateNewMax(Y2Scale, YValue);
					if (YValue < Y2Scale.min)
						calculateNewMin(Y2Scale, YValue);
					continue;
				}
				if (YValue > YScale.max)
					calculateNewMax(YScale, YValue);
				if (YValue < YScale.min)
					calculateNewMin(YScale, YValue);
			}

		}
	}

	private void calculateNewMax(Scale s, double m)
	{
		if (!s.exactMaxValue)
		{
			s.max = m;
			return;
		}
		if (s.preferred_MaxMin_values != null && s.preferred_MaxMin_values.length > 0)
		{
			int j = 0;
			do
			{
				if (j >= s.preferred_MaxMin_values.length)
					break;
				if (s.preferred_MaxMin_values[j] >= m)
				{
					s.max = s.preferred_MaxMin_values[j];
					break;
				}
				j++;
			} while (true);
		}
	}

	protected Scale getActiveXScale(DataSerie s)
	{
		Scale scale = XScale;
		if (s.secondaryXAxis != null)
			scale = s.secondaryXAxis.scale;
		return scale;
	}

	protected Scale getActiveYScale(DataSerie s)
	{
		Scale scale = YScale;
		if (s.secondYAxis && Y2Scale != null)
			scale = Y2Scale;
		else
		if (s.secondaryYAxis != null)
			scale = s.secondaryYAxis.scale;
		return scale;
	}

	private void calculateNewMin(Scale s, double m)
	{
		if (!s.exactMinValue)
		{
			s.min = m;
			return;
		}
		if (s.preferred_MaxMin_values != null && s.preferred_MaxMin_values.length > 0)
		{
			int j = s.preferred_MaxMin_values.length - 1;
			do
			{
				if (j <= 0)
					break;
				if (s.preferred_MaxMin_values[j] <= m)
				{
					s.min = s.preferred_MaxMin_values[j];
					break;
				}
				j--;
			} while (true);
		}
	}

	protected boolean inSameSubChart(DataSerie tmpSerie, DataSerie s)
	{
		boolean usingStackAxis = false;
		if (s.secondaryYAxis != null && s.secondaryYAxis.mainAxis.stackAdditionalAxis)
			usingStackAxis = true;
		if (tmpSerie.secondaryYAxis != null && tmpSerie.secondaryYAxis.mainAxis.stackAdditionalAxis)
			usingStackAxis = true;
		return tmpSerie.secondaryXAxis == s.secondaryXAxis && (tmpSerie.secondaryYAxis == s.secondaryYAxis || !usingStackAxis);
	}
}
